<?php
namespace Backend\Modules\User\Controllers;
use Backend\Modules\User\Forms\FormCreateUser;
use Backend\Modules\User\Forms\FormLogin;
use Backend\Modules\User\Models\Users as Users;
use Backend\Modules\User\Models\UsersProfile as UsersProfile;

class UserController extends \BackendController
{
    public function indexAction()
    {
        $this->view->users = Users::find();
    }
    
    public function loginAction()
    {
        if($this->auth->isUserSignedIn())
        {
            return $this->response->redirect($this->getDi()->get('config')->application->backendRedirectUri);
        }
        
        $form = new FormLogin();

        try {
            if (!$this->request->isPost()) {
                
            } else {
                if ($form->isValid($this->request->getPost()) == false) {
                    foreach ($form->getMessages() as $message) {
                        $this->flash->error($message);
                    }
                } else {
                    $this->auth->check(array(
                        'email' => $this->request->getPost('email'),
                        'password' => $this->request->getPost('password'),
                        'remember' => $this->request->getPost('remember')
                    ));

                    return $this->response->redirect('users');
                }
            }
        } catch (\Exception $e) {
            $this->flash->error($e->getMessage());
        }

        $this->view->form = $form;
    }
    
    /*
     * create user
     */
    public function createAction()
    {
        $form = new FormCreateUser();

        if ($this->request->isPost()) {
            if ($form->isValid($this->request->getPost()) != false) {
                $user = new Users();

                $user->assign(array(
                    'name' => $this->request->getPost('name'),
                    'email' => $this->request->getPost('email'),
                    'password' => $this->security->hash($this->request->getPost('password')),
                    'group_id' => 5,
                    'banned' => 'N',
                    'suspended' => 'N',
                    'active' => 'Y',
                    'created_at' => date("Y-m-d H:i:s"),
                    'updated_at' => date("Y-m-d H:i:s")
                ));

                if ($user->save()) {

                    $userProfiles = new UsersProfile();
                    $userProfiles->assign(array(
                        'users_id' => $user->getId(),
                        'created_at' => date("Y-m-d H:i:s"),
                        'updated_at' => date("Y-m-d H:i:s")
                    ));

                    $userProfiles->save();

                    return $this->dispatcher->forward(array(
                            'controller' => 'user',
                            'action' => 'index'
                    ));
                }

                $this->flash->error($user->getMessages());
            }
        }

        $this->view->form = $form;
    }
    
    /*
     * user logout
     */
    
    public function logoutAction()
    {
        $this->auth->remove();
        return $this->response->redirect();
    }
}
